#include<iostream>
#include"comm.hpp"
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<string>
int main()
{
    umask(0);
    int n = mkfifo(FIFO_FILE,0666);
    if(n<0)
    {
        std::cerr << "mkfifo error" << std::endl;
        return 1;
    }
    std::cout<<"mkfifo success"<<std::endl;

    int fd = open(FIFO_FILE,O_RDONLY);
    if(fd<0)
    {
        std::cout << "open fifo error" << std::endl;
        return 2;
    }
    std::cout << "open fifo success!" << std::endl;

    while(true)
    {
        char buffer[1024];
        int number = read(fd,buffer,sizeof(buffer)-1);
        if(number>0)
        {
            buffer[number]=0;
            std::cout<<"client say#"<<buffer<<std::endl;
        }
        else
        {
            //todo
        }
    }

    close(fd);

    n = unlink(FIFO_FILE);
    if(n==0)
    {
        std::cout<<"unlink fifo success"<<std::endl;
    }
    else{
        std::cout<<"unlink fifo fail!"<<std::endl;
    }
    return 0;
} 